<template>
  <div id="lopgin">
   <img class="bakg" src="https://leader.yaetong.com/assets/img/635f0ad83891a7135e014d0e776913c.fc30824a.jpg"  alt="">

    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      status-icon
      :rules="rules"
      class="ruleForm"
    >
    <div class="title" style="margin-bottom:25px;font-weight:500">欢迎登录牙E通口腔门诊管理系统</div>
      <el-form-item label="账号" prop="username">
        <el-input v-model="ruleForm.username" type="text" autocomplete="off" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="ruleForm.password"
          type="password"
          autocomplete="off"
        />
      </el-form-item>
      <el-form-item label="验证码" prop="captcha" class="captcha_cover" style="position:relative;">
        <el-input
          v-model="ruleForm.captcha"
          @keydown.enter="submitForm(ruleFormRef)"
          class="last"
          maxlength="5"
        />
       
        <!-- <div class="box"> -->
          <i v-loading="flag" v-html="captcha" @click="getCaptchaImg" style="height:28px;px;cursor:pointer;border-right:1px solid white;border-top:1px solid white;border-bottom:1px solid white;border-radius:4px;border-top-left-radius:0px;border-bottom-left-radius:0px;position:absolute;right:15px;"></i>
        <!-- </div> -->
      </el-form-item>
      <div style="font-size:8px;margin-right:41px;color:black" >登录诊所:立登尔东大门诊部登录</div>
      <el-form-item>
        <el-button
          type="primary"
          class="submit"
          @click="submitForm(ruleFormRef)"
        >
          Submit
        </el-button>
      </el-form-item>
      <div class="tt">技术服务:通通健康</div>
    </el-form>
    
  </div>
</template>

<script setup>
import { reactive, ref } from "@vue/reactivity";
// import type { FormInstance, FormRules } from "element-plus";
import { loginApi } from "../../api/users.js";
import { useRouter } from "vue-router";
import useUser from "../../store/user.js";
import * as api from "@/api/users.js";
import { onMounted } from "vue";
import { ElLoading } from 'element-plus'
import { ElNotification } from 'element-plus'
import usepermission from "@/store/permission.js";
import useRole from "@/store/role.js";
let role = useRole();
let permission = usepermission();

const ruleFormRef = ref();
let user = useUser();
let router = useRouter();

const flag = ref(false);
const checkcaptcha = (rule, value, callback) => {
  let reg = /^[a-zA-Z0-9_-]{3,16}$/;
  if (reg.test(value)) {
    callback();
  } else {
    callback("大小写字母,数字,下划线,中划线,3到16位");
  }
};

const validateusername = (rule, value, callback) => {
  if (value) {
    callback();
  } else {
    callback("输入密码不能为空");
  }
};
const validateusername2 = (rule, value, callback) => {
  if (value) {
    callback();
  } else {
    callback("验证码不能为空");
  }
};

const ruleForm = reactive({
  username: "",
  password: "",
  captcha: "",
});

const captcha = ref("");

let getCaptchaImg = async()=>{
  try {
    flag.value = true
    let res = await api.getCaptchaApi()
    //  console.log(res);
    captcha.value = res.data.img
    flag.value = false
  } catch (error) {
    console.log(error);
    flag.value = false
  }
}


onMounted(()=>{
  getCaptchaImg()
})

//校验验证码
const checkCaptcha =async (captcha)=>{
   typeof(captcha) === "string" && (captcha = captcha.toLowerCase())
    try {
      let res = await api.verifyCaptchaApi(captcha)
      return res.data.state
    } catch (error) {
      console.log(error);
      return false
    }
}

const rules = reactive({
  username: [{ validator: validateusername, trigger: "blur" }],
  password: [{ validator: validateusername2, trigger: "blur" }],
  captcha: [{ validator: checkcaptcha, trigger: "blur" }],
});


const submitForm = (formEl) => {
  if (!formEl) return;
  formEl.validate(async (valid) => {
    if (valid) {
      const loading = ElLoading.service({ 
        // fullscreen: true,
        lock: true,
        text:"正在加载中...",
        background: 'rgba(0, 0, 0, 0.5)',
       })
      
      try {
        const previewCaptcha = await checkCaptcha(ruleForm.captcha)
        if(previewCaptcha){
          let res = await api.loginApi(ruleForm.username,ruleForm.password)
            console.log(res);
            user.$state.userInfo.value = res.data.userInfo
            user.$state.permission.value = res.data.permission

            //获取当前登录权限
            // console.log(user.userInfo.value.roleid);
              permission.getCurrentRolePermission(user.userInfo.value.roleid)

            //获取所有角色
             role.getAllRole()
            // console.log(role);
            sessionStorage.setItem("token",res.data.token)
            router.push("/")
            ElNotification({
              title:"登录成功",
              message:res.data.msg,
              type:"success",
              duration:2000,
            })
            
        }
        loading.close()
      } catch (error) {
        console.log(error);
        loading.close()
      }
    } else { 
      return false;
    }
  });
};

const resetForm = (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>
<style scoped>
/* .video-background {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  object-fit: cover;
  width: 100%;
  height: 100%;
  z-index: -999;
} */
.bakg{
  height: 100%;
}
.ruleForm .el-input {
  width: 250px;
  /* margin-right: 80px; */
}
.ruleForm {
  position: fixed;
  top: 250px;
  right: 200px;
  border: 2px solid #ccc;
  background-color: rgba(195, 215, 223, 0.3);
  border-radius: 20px;
  height: 280px;
  width: 400px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding-top: 30px;
}
.submit {
  /* margin-right: 120px; */
  margin-top: 20px;
  width: 100px;
  height: 30px;
}
.last {
  width: 250px !important;
  border-right: none !important;
  border-top-right-radius: none !important ;
  border-bottom-right-radius: none !important ;
  --el-border-color:none!important;
  position: relative;
  margin-right: 13px;

}
:deep(svg){
  height: 28px;
  width: 80px;
  background-color: white;
}
:deep(.captcha){
    width: 70px;
    height: 32px;
    background-color: white!important;
    /* border-radius: 4px; */
   
   right: 4px;

    /* border-color: 2px solid #dcdfe6!important;*/
  }
  /* :deep(element-loading-svg){
    width: 20px;
    height: 20px;
  } */
  .tt{
    position: fixed;
    right: 320px;
    bottom: 20px;
    color: #aaa;
  }
</style>
